const mongoose=require('mongoose');
const auto=require('mongoose-auto-increment');
const connection=mongoose.connect('mongodb://localhost:27017/autoIncrement',{useMongoClient:true});

mongoose.Promise=global.Promise;
auto.initialize(connection);



const bookSchema=new mongoose.Schema({
    title:String
});

bookSchema.plugin(auto.plugin,'Book');

const Book=mongoose.model('Book',bookSchema);


Book.nextCount((err,count)=>{
    console.log('start nextCount:',err,count);
    let book=new Book({title:'Book-'+Date.now()});
    book.save((err,ret)=>{
        console.log('save:',err,ret);
       book.nextCount((err,count)=>{
           console.log('saved nextCount:',err,count);
           process.exit();
       });
    });
});
